﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: Group.cs
#endregion

using System;
using System.Data;
using csharp.common;
using System.Collections.Generic;

namespace csharp.rad.finan
{
	/// <summary>
	/// Description of Group.
	/// </summary>
	public class Group : DefaultClass, IClass
	{
		//private String   _id;
		private String   _code;
		private String   _name;
		private DateTime _register_date;
		private bool     _active;
		
		public Group()
		{
			gsql.table= "finan.group";
			_sequence = "finan.group_group_id_seq";
		}
		
		
		public String code
		{
			set { _code = value; }
			get { return  _code; }
		}
		
		public String name
		{
			set { _name = value; }
			get { return  _name; }
		}
		
		public DateTime registerDate
		{
			set { _register_date = value; }
			get { return  _register_date; }
		}
		
		public bool active
		{
			set { _active = value; }
			get { return  _active; }
		}
		
		/// <summary>
		/// Clear the object atributes.
		/// </summary>	
		public void clear()
		{
			_id            = String.Empty;
			_code          = String.Empty;
			_name          = String.Empty;
			_register_date = DateTime.Now;
			_active        = true;
		}
	
	
		/// <summary>
		/// Fill the class with data of DataRow
		/// </summary>
		/// <param name="dr">DataRow with the data to fill the class.</param>				
		public override void get(DataRow dr)
		{
			_id       		= dr["group_id"].ToString();
			_code     		= dr["group_code"].ToString();
			_name     		= dr["group_name"].ToString();
			_register_date = Convert.ToDateTime(dr["group_register_date"]);
			_active   		= Convert.ToBoolean(dr["group_active"]);         
		}
		
		
		public override void setGsql()
		{
			gsql.setValue(_id, "group_id");
			gsql.setValue(_code, "group_code");
			gsql.setValue(_name, "group_name");
			//gsql.setValue(_register_date.ToString("dd/MM/yyyy"), "group_register_date");
			gsql.setValue(_active.ToString(), "group_active");
		}
	}


	//List
	public class Groups : DefaultList, IList
	{
		public Groups()
		{
			gsql.table= "finan.group";
		}
		
		public Group get(Int32 index)
		{
			return (Group)items[index];
		}
		
		public Group get()
		{
			return (Group)items[index];
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY group_code";
			
			return mountList();
		}
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			items.Clear();
			tam= gsql.getRowCount();
			for (int i=0; i < tam; i++)
			{
				Group g = new Group();
				g.get(gsql.getRow(i));
				items.Add(g);
			}
			return true;
		}
	}
}
